using System.Collections;
using System.Collections.Generic;
using ExitGames.Client.Photon;
using UnityEngine;

public class CopsAndRobbersController : MonoBehaviour
{
	public static CopsAndRobbersController instance;

	public List<CRGate> gates;

	public bool gameIsStarted;

	public int gatesDestroyed;

	public bool pickedUpBonus;

	public bool isInRobbersSkin;

	private CopsRobbersSettings cRSettings;

	private GameObject inventoryBonus;

	private IEnumerator interfaceUpdate;

	private IEnumerator healthUpdate;

	private void OnEnable()
	{
		instance = this;
		PVPManager.onPVPStarted += CheckForPlayerCountChange;
	}

	private void OnDisable()
	{
		PVPManager.onPVPStarted -= CheckForPlayerCountChange;
	}

	private void Start()
	{
		cRSettings = (CopsRobbersSettings)settings.GetSettings(TypeGame.CopsAndRobbers);
	}

	private void StartGame()
	{
		if (gameIsStarted)
		{
			return;
		}
		gameIsStarted = true;
		if (TDMController.myTeam == PunTeams.Team.blue)
		{
			BCWDebug.Log("Your team is Blue (robbers)");
			if (GetCurrentStage() == 0)
			{
				SpawnInventoryBonus();
				MarketController.Instance.SetWeaponSetForRobbers(false);
				isInRobbersSkin = true;
			}
			else
			{
				MarketController.Instance.SetWeaponSetForRobbers(true);
			}
		}
		else
		{
			if (TDMController.myTeam != PunTeams.Team.red)
			{
				gameIsStarted = false;
				return;
			}
			BCWDebug.Log("Your team is Red (cops)");
			MarketController.Instance.SetWeaponSetForCops();
			isInRobbersSkin = false;
		}
		BCWDebug.Log("Start Cops n Robbers");
		if (!PVPManager.instance.timerStart)
		{
			SetGatesMaxHealth();
		}
		CopsAndRobbersInterface.instance.ResetUI(gates);
		CopsAndRobbersInterface.instance.UpdateAllInterface(gates);
		if (interfaceUpdate != null)
		{
			StopCoroutine(interfaceUpdate);
		}
		interfaceUpdate = InterfaceUpdate();
		StartCoroutine(interfaceUpdate);
		GameController.instance.CurrentGameMode.StartRound();
		GameInfoWindow.instance.UpdateTitle();
		StartCoroutine(CopsAndRobbersInterface.instance.ShowNotification(TDMController.myTeam, 0, 9f));
	}

	public void SetPlayerInventoryAccordingToTeam(bool needEquipAll)
	{
		if (TDMController.myTeam == PunTeams.Team.blue && !isInRobbersSkin)
		{
			MarketController.Instance.SetWeaponSetForRobbers(GetCurrentStage() != 0 || needEquipAll);
			isInRobbersSkin = true;
		}
		else if (TDMController.myTeam == PunTeams.Team.red && isInRobbersSkin)
		{
			MarketController.Instance.SetWeaponSetForCops();
			Object.Destroy(inventoryBonus);
			isInRobbersSkin = false;
		}
	}

	public void Restart()
	{
		Debug.Log("Restart Cops And Robbers");
		gameIsStarted = false;
		gatesDestroyed = 0;
		pickedUpBonus = false;
	}

	public void ResetLevel()
	{
		SetGatesMaxHealth();
		RespawnGates();
		CopsAndRobbersInterface.instance.ResetUI(gates);
	}

	public int GetCurrentStage()
	{
		for (int i = 0; i < gates.Count; i++)
		{
			if (!gates[i].opend)
			{
				return i;
			}
		}
		return -1;
	}

	public CRGate GetCurrentGate()
	{
		int currentStage = GetCurrentStage();
		if (currentStage == -1)
		{
			return null;
		}
		return gates[currentStage];
	}

	public void FinishGame()
	{
		PVPManager.instance.TimeOut();
		EndGame();
	}

	private void RespawnGates()
	{
		foreach (CRGate gate in gates)
		{
			gate.Respawn();
		}
	}

	private void SetGatesMaxHealth()
	{
		foreach (CRGate gate in gates)
		{
			gate.maxHealth = cRSettings.boilerToGateHealth[Mathf.Clamp(controllerConnectPhoton.thisScript.playerBoiler, 0, cRSettings.boilerToGateHealth.Count - 1)];
			gate.health = gate.maxHealth;
		}
	}

	public int GetRespawnTime(PunTeams.Team playerTeam)
	{
		return cRSettings.GetRespawnTime(playerTeam, GetCurrentStage());
	}

	private IEnumerator InterfaceUpdate()
	{
		while (true)
		{
			CopsAndRobbersInterface.instance.UpdateAllInterface(gates);
			yield return new WaitForSeconds(0.5f);
		}
	}

	public void EndGame()
	{
		if (PhotonNetwork.isMasterClient)
		{
			if (healthUpdate != null)
			{
				StopCoroutine(healthUpdate);
			}
			StartCoroutine(ResetRoom());
		}
		Object.Destroy(inventoryBonus);
		TDMController.instance.SetStartPVP(false);
	}

	private IEnumerator ResetRoom()
	{
		yield return new WaitForSeconds(1f);
		PhotonNetwork.room.SetCustomProperties(new ExitGames.Client.Photon.Hashtable { { "StartRoundTime", null } });
		yield return new WaitForSeconds(2f);
		ResetLevel();
	}

	public void SpawnInventoryBonus()
	{
		if (TDMController.myTeam == PunTeams.Team.blue && inventoryBonus == null)
		{
			inventoryBonus = bonuseManager.thisScript.InstantLocalBonuse(idBonuse.RobberInventory, ((CopsRobbersSettings)settings.GetSettings(TypeGame.CopsAndRobbers)).GetBonusSpawnPoint());
		}
	}

	public void PickUpInventoryBonus()
	{
		pickedUpBonus = true;
		PlayerBehavior.MyPlayer.equipmentManager.EquipAllItems();
		MarketController.Instance.GetStartWeapons();
		Object.Destroy(inventoryBonus);
	}

	private void CheckForPlayerCountChange()
	{
		if (healthUpdate != null)
		{
			StopCoroutine(healthUpdate);
		}
		healthUpdate = PlayerCountChange();
		StartCoroutine(healthUpdate);
	}

	private IEnumerator PlayerCountChange()
	{
		if (!PhotonNetwork.isMasterClient)
		{
			yield break;
		}
		float maxHp = cRSettings.boilerToGateHealth[Mathf.Clamp(controllerConnectPhoton.thisScript.playerBoiler, 0, cRSettings.boilerToGateHealth.Count - 1)];
		int lastCount = 0;
		while (true)
		{
			yield return new WaitForSeconds(1f);
			if (TDMController.instance.CountTeam(PunTeams.Team.blue) == 0 || !gameIsStarted)
			{
				continue;
			}
			float hpCoef = (float)TDMController.instance.CountTeam(PunTeams.Team.blue) / ((float)cRSettings.maxPlayers / 2f);
			if (lastCount == TDMController.instance.CountTeam(PunTeams.Team.blue))
			{
				continue;
			}
			foreach (CRGate gate in gates)
			{
				int oldMaxHp = gate.maxHealth;
				gate.maxHealth = Mathf.CeilToInt(maxHp * hpCoef);
				gate.health = Mathf.FloorToInt((float)gate.health * (float)gate.maxHealth / (float)oldMaxHp);
			}
			lastCount = TDMController.instance.CountTeam(PunTeams.Team.blue);
			CopsAndRobbersInterface.instance.ResetUI(gates);
		}
	}

	private void Update()
	{
		if (GameController.instance.curTypeGame != TypeGame.CopsAndRobbers)
		{
			return;
		}
		bool flag = false;
		if (PhotonNetwork.room != null && PhotonNetwork.room.customProperties.ContainsKey("timerStarted"))
		{
			flag = (bool)PhotonNetwork.room.customProperties["timerStarted"];
		}
		if (PhotonNetwork.connected && PlayerBehavior.MyPlayer != null && !gameIsStarted && (PVPManager.instance.CanStart() || (flag && TDMController.myTeam != 0)))
		{
			StartGame();
		}
		if (cRSettings.timeOutWhenLeave && gameIsStarted && PVPManager.instance.gameIsStarted && TDMController.myTeam != 0)
		{
			if (TDMController.instance.CountTeam(PunTeams.Team.blue) == 0 && GameController.instance.playerScript.startPVP)
			{
				FinishGame();
			}
			if (TDMController.instance.CountTeam(PunTeams.Team.red) == 0 && GameController.instance.playerScript.startPVP)
			{
				FinishGame();
			}
		}
	}
}
